using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace TPDSControl
{
    [Serializable()]
    public class Image
    {
        public enum ImageStatus
        {
            OK,
            Error,
            Processed
        }

        public string Filename;
        public string ThumbnailFilename;
        public string BarcodeNo; // = Goods ID
        public bool IsBarcode;
        public string ConsignmentNo;
        public string ErrorMessage;
        public ImageStatus Status;
        public bool IsWaiting;
        [XmlIgnore()]
        public string ImagesetFilepath;

        public void Delete(string serverPath)
        {
            FileInfo file = null;
            try
            {
                file = new FileInfo(Path.Combine(serverPath,Filename));
                if (file.Exists)
                {
                    file.Delete();
                }

                file = new FileInfo(Path.Combine(serverPath, ThumbnailFilename));
                if (file.Exists)
                {
                    file.Delete();
                }

                Status = ImageStatus.Processed;
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, Filename);
                throw (ex);
            }
        }

        public void GenerateNewFilename(string serverPath, string suffix)
        {
            FileInfo file = null;
            try
            {
                string waitingImageName = string.Empty;
                string waitingImageName_thumb = string.Empty;

                file = new FileInfo(Path.Combine(serverPath, Filename));
                if (file.Exists)
                {
                    waitingImageName = ConsignmentNo.Replace("*","") + suffix + file.Extension;
                    if (File.Exists(Path.Combine(file.DirectoryName, waitingImageName)))
                    {
                        waitingImageName = Guid.NewGuid().ToString() + file.Extension;
                    }
                    this.Filename = this.Filename.ToLower().Replace(file.Name.ToLower(), waitingImageName);
                    file.MoveTo(Path.Combine(file.DirectoryName, waitingImageName));
                }

                file = new FileInfo(Path.Combine(serverPath, ThumbnailFilename));
                if (file.Exists)
                {
                    waitingImageName_thumb = ConsignmentNo.Replace("*", "") + suffix + "_thumb" + file.Extension;
                    if (File.Exists(Path.Combine(file.DirectoryName, waitingImageName_thumb)))
                    {
                        waitingImageName_thumb = Guid.NewGuid().ToString() + file.Extension;
                    }
                    this.ThumbnailFilename = this.ThumbnailFilename.ToLower().Replace(file.Name.ToLower(), waitingImageName_thumb);
                    file.MoveTo(Path.Combine(file.DirectoryName, waitingImageName_thumb));
                }

            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, Filename);
                throw (ex);
            }
        }

        public void DeleteFromImageset()
        {
            ImageSet imageset = new ImageSet();
            imageset = ImageSet.LoadOne( ImagesetFilepath );
            if( imageset == null )
                return;

            foreach (Image image in imageset.Images)
            {
                if (image.Filename == this.Filename)
                {
                    imageset.Images.Remove(image);
                    if (imageset.Images.Count == 0)
                    {
                        imageset.Delete();
                    }
                    else
                    {
                        imageset.Save();
                    }
                    return;
                }
            }
        }

        public void SetAsBarcode(bool isBarcode)
        {
            ImageSet imageset = new ImageSet();
            imageset = ImageSet.LoadOne( ImagesetFilepath );
            if( imageset == null )
                return;

            foreach (Image image in imageset.Images)
            {
                if (image.Filename == this.Filename)
                {
                    if (isBarcode)
                    {
                        image.IsBarcode = true;
                    }
                    else
                    {
                        image.IsBarcode = false;
                    }
                }
            }
            imageset.Save();
        }

        public void Move(string serverPath, string targetfilepath)
        {
            FileInfo file = null;
            try
            {
                file = new FileInfo(Path.Combine(serverPath,ThumbnailFilename));
                if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, ThumbnailFilename);
            }

            try
            {
                file = new FileInfo(Path.Combine(serverPath,Filename));
                if (file.Exists)
                {
                    file.MoveTo(targetfilepath);
                    Status = ImageStatus.Processed;
                }
            }
            catch (Exception ex)
            {
                TPDSConfig.ErrorLog.WriteError(ex.Message, Filename);
            }            
        }

    }
}
